<?php

/**
 * 文件注释
 *
 * Author: Liam
 * Datetime: 11/26/17 00:00 PM
 */

namespace ApiBundle\Controller;

use BaseBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;

class BannerController extends BaseController
{
    // banner列表
    public function bannerListAction(Request $request)
    {
        $conn = $this->get('database_connection');

        $data['bannerList'] = $conn->fetchAll("SELECT * FROM banner");
        return $this->jsonResponse('操作成功', 0, $data);
    }

    // 新增banner，编辑banner
    public function operaBannerAction(Request $request)
    {
        $id = $request->get('id');
        $title = $request->get('title');
        $thumb = $request->get('thumb');
        $sort_order = $request->get('sort_order');
        $url = $request->get('url');
        $thumb_root = $request->getSchemeAndHttpHost().''.$request->getBasePath().'/'.$thumb;

        $data = array(
            'title' => $title,
            'image_path' => $thumb,
            'h_image_path' => $thumb_root,
            'sort_order' => $sort_order,
            'url' => $url
        );

        $conn = $this->get('database_connection');
        if ($id === null) {
            $data['created_at'] = date('y-m-d h:i:s',time());
            $conn->insert('banner', $data);
        } else {
            $data['updated_at'] = date('y-m-d h:i:s',time());
            $conn->update('banner', $data, array('id'=> $id));
        }

        return $this->jsonResponse('操作成功', 0);
    }

    //  禁用(解禁)banner
    public function prohibitBannerAction(Request $request)
    {
        $id = $request->get('id');
        if ($id == null) {
            return $this->jsonResponse('缺少参数', 1);
        }

        $conn = $this->get('database_connection');
        $data = $conn->fetchAssoc("SELECT * FROM banner WHERE id = '". $id ."'");
        $data['status'] = $data['status'] == 1 ? 0 : 1;

        $conn->update('banner', $data, array('id'=>$id));

        return $this->jsonResponse('操作成功', 0);
    }

    //  删除banner
    public function delBannerAction(Request $request)
    {
        $id = $request->get('id');
        if ($id == null) {
            return $this->jsonResponse('缺少参数', 1);
        }

        $conn = $this->get('database_connection');
        $conn->delete('banner', array('id' => $id));

        return $this->jsonResponse('操作成功', 0);
    }
}